package com.zhr.http.demo.api;

import com.smartframework.web.core.mq.bean.MqMsgBean;
import com.smartframework.web.core.runner.CommandExecutor;
import com.smartframework.web.core.runner.ExecutorType;
import com.smartframework.web.core.runner.HandleStyle;
import com.smartframework.web.server.AbstractExecutor;
import com.zhr.scada.emsg.rpc.EmsgRestClient;

import java.util.HashMap;
import java.util.Map;

/**
 * 继承AbstractExecutor，并实现其execute执行器函数
 * ① 必须指定注解 CommandExecutor，注解必须标识 uri、commandType=HTTP_PROXY
 * ② 如果返回值直接响应给第三方则 ignoreWrapper 设置为true，否则保持默认值false将按照SmartView格式返回；为true时通常 handleStyle=BIO，返回执行结果
 * ③ 执行结果是否直接返回，BIO为服务端按线程内处理完成并响应，NIO为处理完成前已经返回，通常用于接受指令执行后按异步方式通知第三方（MQ与Http的有机结合）
 *
 * @author LIUJUN
 */
@CommandExecutor(uri = "/demo/pas/calculator",
        ignoreWrapper = true,
        commandType = ExecutorType.HTTP_PROXY,
        handleStyle = HandleStyle.BIO)
public class PasCalculateHandler extends AbstractExecutor {
    @Override
    public Object execute(Map<String, Object> map) throws Exception {
        Map<String, Object> response = new HashMap<>();
        response.put("field1", 1);
        response.put("lines", new Integer[]{1, 2, 3, 4, 5});
        //模拟通过消息方式异步转同步调用
        MqMsgBean mqMsgBean = new MqMsgBean();
        mqMsgBean.setTopic("MY_DEMO_REQUEST");
        mqMsgBean.setMsgBody(new HashMap<String, Object>() {{
            put("feeder", 10000L);
            put("stationId", 100);
        }});

        EmsgRestClient emsgRestClient = new EmsgRestClient();
        Object result = emsgRestClient.sendRequest(mqMsgBean);
        System.out.println(emsgRestClient.getId() + " 调用结果：");
        System.out.println(result);
        response.put("rpcResult", result);

        return response;
    }
}
